/* 
 * File:   Matriz.cpp
 * Author: agu
 * 
 * Created on 14 de junio de 2014, 1:23
 */
#include "client_Directories.h"
#include "client_TileMap.h"
#include <iostream>
#include <cstdlib>
#define BLOCKSIZE 32
const string pathFloor = DIR_IMG_MAPS_ITEMS + "floor.png";
const string pathBox = DIR_IMG_MAPS_ITEMS + "box.png";
const string pathBoxE = DIR_IMG_MAPS_ITEMS + "box.png";
const string pathMetal = DIR_IMG_MAPS_ITEMS + "metal.png";
const string pathZombieG = DIR_IMG_SPRITES + "zombieGreen.png";
const string pathZombieR = DIR_IMG_SPRITES + "zombieRed.png";
const string pathZombieB = DIR_IMG_SPRITES + "zombieBlue.png";
const string pathFriend = DIR_IMG_SPRITES + "friend.png";
const string pathPowerR = DIR_IMG_SPRITES + "red.png";
const string pathPowerB = DIR_IMG_SPRITES + "blue.png";
const string pathPowerG = DIR_IMG_SPRITES + "green.png";
const string pathPowerY = DIR_IMG_SPRITES + "yellow.png";
const string pathPowerBl = DIR_IMG_SPRITES + "black.png";
const string pathBomb = DIR_IMG_SPRITES + "bomb.png";
const string pathBombP = DIR_IMG_SPRITES + "bombPlant.png";
const string pathExplosion = DIR_IMG_SPRITES + "explosionBomba.png";

TileMap::TileMap() {
    this->renderer = renderer;
    this->numColumns = this->numRows = 0;
    this->posX = this->posY = 0;
    this->pathPlayer = "";

}

TileMap::TileMap(SDL_Renderer* renderer) {
    this->renderer = renderer;
    this->numColumns = this->numRows = 0;
    this->posX = this->posY = 0;
    this->pathPlayer = "";
}

void TileMap::load(const string tileMap) {
    this->numColumns = sqrt(tileMap.size());
    this->numRows = this->numColumns;
    int tileIndex = 0;
    for (unsigned int i = 0; i < this->numColumns; i++) {
        for (unsigned int j = 0; j < this->numRows; j++) {
            tileIndex = (numRows - 1) - i + j * (numRows - 1) + j;
            this->loadByCode(tileMap.at(tileIndex), i, j);

        }
    }

}

void TileMap::update(const string tileMap) {
    this->free();
    this->numColumns = sqrt(tileMap.size());
    this->numRows = this->numColumns;
    int tileIndex = 0;
    for (unsigned int i = 0; i < this->numColumns; i++) {
        tileIndex = numColumns - (1 + i);
        for (unsigned int j = 0; j < this->numRows; j++) {
            tileIndex = (numRows - 1) - i + j * (numRows - 1) + j;
            this->loadByCode(tileMap.at(tileIndex), j, i);
        }
    }

}

void TileMap::render() {

    for (unsigned int i = 0; i < this->staticItems.size(); i++) {
        this->staticItems.at(i)->render();
    }
    for (unsigned int i = 0; i < this->dinamicItems.size(); i++) {
        this->dinamicItems.at(i)->render();
    }
}

void TileMap::free() {

    for (unsigned int i = 0; i < this->staticItems.size(); i++) {
        delete this->staticItems.at(i);
    }
    for (unsigned int i = 0; i < this->dinamicItems.size(); i++) {
        delete this->dinamicItems.at(i);
    }
    this->staticItems.clear();
    this->dinamicItems.clear();
    this->pathPlayer = "";
    this->explotion = false;
}

TileMap::~TileMap() {
    this->free();
    this->renderer = NULL;
}

void TileMap::getPlayerPosition(int& x, int&y) {
    x = this->posX;
    y = this->posY;
}

void TileMap::setRenderer(SDL_Renderer* renderer) {
    this->renderer = renderer;
}

string TileMap::getPlayerPath() {
    return this->pathPlayer;
}

bool TileMap::havePlayer() {
    if (this->pathPlayer.empty())
        return false;
    return true;
}

bool TileMap::haveExplotion() {
    return this->explotion;
}

void TileMap::setPlayerCode(const string code) {
    this->playerCode = code;
}

void TileMap::identifPlayer(string codTileMap, unsigned int i, unsigned int j) {

    if ((codTileMap == "a") || (codTileMap == "f") || (codTileMap == "k") || (codTileMap == "p")){
        //Rojo
        if (codTileMap == this->playerCode) {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            this->posX = i*BLOCKSIZE;
            this->posY = j*BLOCKSIZE;
            this->pathPlayer = pathPowerR;
        } else {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            Animation* powerR = new Animation(this->renderer, pathPowerR, i*BLOCKSIZE, j * BLOCKSIZE);            
            powerR->updateDirection(codTileMap, "f", "a", "p", "k");
            this->dinamicItems.push_back(powerR);

        }
    } else if ((codTileMap == "b") || (codTileMap == "g") || (codTileMap == "l") || (codTileMap == "q")){
        //Verde
        if (codTileMap == this->playerCode) {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            this->posX = i*BLOCKSIZE;
            this->posY = j*BLOCKSIZE;
            this->pathPlayer = pathPowerG;
        } else {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            Animation* powerG = new Animation(this->renderer, pathPowerG, i*BLOCKSIZE, j * BLOCKSIZE);
            powerG->updateDirection(codTileMap, "g", "b", "q", "l");
            this->dinamicItems.push_back(powerG);
        }
    } else if ((codTileMap == "c") || (codTileMap == "h") || (codTileMap == "m") || (codTileMap == "r")) {
        //Azul
        if (codTileMap == this->playerCode) {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            this->posX = i*BLOCKSIZE;
            this->posY = j*BLOCKSIZE;
            this->pathPlayer = pathPowerB;
        } else {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            Animation* powerB = new Animation(this->renderer, pathPowerB, i*BLOCKSIZE, j * BLOCKSIZE);
            powerB->updateDirection(codTileMap, "h", "c", "r", "m");
            this->dinamicItems.push_back(powerB);
        }
    } else if ((codTileMap == "d") || (codTileMap == "i") || (codTileMap == "n") || (codTileMap == "s")){
        //Negro
        if (codTileMap == this->playerCode) {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            this->posX = i*BLOCKSIZE;
            this->posY = j*BLOCKSIZE;
            this->pathPlayer = pathPowerBl;
        } else {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            Animation* powerBl = new Animation(this->renderer, pathPowerBl, i*BLOCKSIZE, j * BLOCKSIZE);
            powerBl->updateDirection(codTileMap, "i", "d", "s", "n");
            this->dinamicItems.push_back(powerBl);
        }
    } else if ((codTileMap == "e") || (codTileMap == "j") || (codTileMap == "o") || (codTileMap == "t")){
        //Amarillo
        if (codTileMap == this->playerCode) {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            this->posX = i*BLOCKSIZE;
            this->posY = j*BLOCKSIZE;
            this->pathPlayer = pathPowerY;
        } else {
            this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
            Animation* powerY = new Animation(this->renderer, pathPowerY, i*BLOCKSIZE, j * BLOCKSIZE);
            powerY->updateDirection(codTileMap, "j", "e", "t", "o");
            this->dinamicItems.push_back(powerY);
        }
    }

}

void TileMap::loadByCode(char codTil, unsigned int i, unsigned int j) {
    string codTileMap(1, codTil);

    if (codTileMap == "0") {
        //Tierra
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if (codTileMap == "1") {
        //Caja
        this->staticItems.push_back(new Texture(this->renderer, pathBox, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if (codTileMap == "2") {
        //Caja con Zombie
        this->staticItems.push_back(new Texture(this->renderer, pathBoxE, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if (codTileMap == "3") {
        //Caja de metal
        this->staticItems.push_back(new Texture(this->renderer, pathMetal, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if ((codTileMap == "4") || (codTileMap == ")") || (codTileMap == "=") || (codTileMap == "?")) {//Aca se detectarian varios codigos correspondiente a un zombie verde
        //Zombie verde
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        Animation* zombieG = new Animation(this->renderer, pathZombieG, i*BLOCKSIZE, j * BLOCKSIZE);
        zombieG->updateDirection(codTileMap, "4", ")", "=", "?");
        this->dinamicItems.push_back(zombieG);
    } else if ((codTileMap == "5") || (codTileMap == "#") || (codTileMap == "!") || (codTileMap == "%")) {//Aca se detectarian varios codigos correspondiente a un zombie azul
        //Zombie Azul
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        //Identificaria al codigo que direccion de movimiento le corresponde y se seteria como currentSprite
        Animation* zombieB = new Animation(this->renderer, pathZombieB, i*BLOCKSIZE, j * BLOCKSIZE);
        zombieB->updateDirection(codTileMap, "5", "#", "!", "%");
        this->dinamicItems.push_back(zombieB);

    } else if ((codTileMap == "6") || (codTileMap == "&") || (codTileMap == "/") || (codTileMap == "(")) {//Aca se detectarian varios codigos correspondiente a un zombie rojo
        //Zombie Rojo
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        Animation* zombieR = new Animation(this->renderer, pathZombieR, i*BLOCKSIZE, j * BLOCKSIZE);
        zombieR->updateDirection(codTileMap, "6", "&", "/", "(");
        this->dinamicItems.push_back(zombieR);
    } else if ((codTileMap == "7") || (codTileMap == "@") || (codTileMap == "·") || (codTileMap == "~")) {
        //Planta
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        Animation* planta = new Animation(this->renderer, pathFriend, i*BLOCKSIZE, j * BLOCKSIZE);
        planta->updateDirection(codTileMap, "7", "@", "·", "~");
        this->dinamicItems.push_back(planta);
    } else if (codTileMap == "8") {
        //Bomba Bomberman
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        this->dinamicItems.push_back(new Animation(this->renderer, pathBomb, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if (codTileMap == "9") {
        //Bomba Planta
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        this->staticItems.push_back(new Texture(this->renderer, pathBombP, i*BLOCKSIZE, j * BLOCKSIZE));
    } else if (codTileMap == "*") {
        //Explosion
        this->staticItems.push_back(new Texture(this->renderer, pathFloor, i*BLOCKSIZE, j * BLOCKSIZE));
        this->staticItems.push_back(new Texture(this->renderer, pathExplosion, i*BLOCKSIZE, j * BLOCKSIZE));
        this->explotion = true;
    } else {
        this->identifPlayer(codTileMap, i, j);
    }
}